<?php
namespace app\modules\member\models;

use Yii;
use yii\base\Model;

class MemberSearch extends Member
{
    public $flag;
    public $key;
    public $begin_time;
    public $end_time;

    public function rules()
    {
        return [
            ['flag', 'in', 'range' => array(1, 2, 3, 4), 'message' => 'flag值必须在1,2,3,4中选择'],
            ['key', 'string', 'message' => 'key必须是字符串'],
            ['state', 'integer', 'message' => 'state必须为数字，或数字字符串'],
            ['state', 'in', 'range' => array(1, 2), 'message' => 'state值必须在1,2中选择'],
            ['begin_time', 'date', 'format'=>'yyyy-MM-dd', 'message' => 'begin_time非yyyy-MM-dd格式'],
            ['end_time', 'date', 'format'=>'yyyy-MM-dd', 'message' => 'end_time非yyyy-MM-dd格式'],
        ];
    }

    public function scenarios()
    {
        return Model::scenarios();
    }

    public function search($params)
    {
        $data['MemberSearch'] = $params;
        // P($data);

        $query = Member::find();

        if (!($this->load($data) && $this->validate())) {
            return false;
        }
        // P($this);

        if(!empty($this->key) and $this->flag == 1){
            $query -> andFilterWhere(['like', 'username', $this->key]);
        }

        if(!empty($this->key) and $this->flag == 2){
            $query -> andFilterWhere(['like', 'nickname', $this->key]);
        }

        if(!empty($this->key) and $this->flag == 3){
            $query -> andFilterWhere(['like', 'phone', $this->key]);
        }

        if(!empty($this->key) and $this->flag == 4){
            $query -> andFilterWhere(['like', 'email', $this->key]);
        }

        if(!empty($this->state)){
            $query -> andWhere([
                '{{%member}}.state' => $this->state,
            ]);            
        }

        if(!empty($this->end_time)){
            $this->end_time .= ' 23:59:59';
        }

        $query -> andFilterWhere(['>=', '{{%member}}.add_time', $this->begin_time])
               -> andFilterWhere(['<=', '{{%member}}.add_time', $this->end_time]);


        return $query;
    }

    
}